package com.bookshop.controller;

import com.bookshop.model.BookModel;
import com.bookshop.model.CatalogModel;
import com.bookshop.model.UserModel;
import com.bookshop.service.BookService;
import com.bookshop.service.CartService;
import com.bookshop.service.CatalogService;
import com.bookshop.service.UserService;
import com.bookshop.vo.BooksVo;
import com.bookshop.vo.CartVo;
import com.bookshop.vo.OrderVo;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Random;

@Controller
public class IndexController {
    @Autowired
    private CatalogService service;
    @Autowired
    private UserService userService;
    @Autowired
    private BookService bookService;
    @Autowired
    private CartService cartService;
    Logger logger = Logger.getLogger(IndexController.class);
    @RequestMapping("/index")
    public String getCatalog(Model model,HttpServletRequest request){

        List<CatalogModel> list = service.findAllCata();
        model.addAttribute("list",list);
        int catalogId = new Random().nextInt(4)+1;
        logger.info(catalogId);
        List<BooksVo> list1 = bookService.findAllBooksById(catalogId);
        HttpSession session = request.getSession();
        String username= (String) session.getAttribute("username");
        Integer userid= (Integer) session.getAttribute("userid");
        model.addAttribute("book",list1);
        model.addAttribute("username",username);
        model.addAttribute("userid",userid);
        UserModel byUserid = userService.findByUserid(userid.toString());
        model.addAttribute("user",byUserid);
        return "WEB-INF/view/index.jsp";

    }
    @RequestMapping("list")
    public String getByCatalogId(Model model, @RequestParam("catalogId") int catalogId, HttpServletRequest request){
        String username = (String)request.getSession().getAttribute("username");
        if(username == null){
            model.addAttribute("message","用户不存在请登录");
            return "WEB-INF/view/login.jsp";
        }
        List<BooksVo> list1 = bookService.findAllBooksById(catalogId);
        model.addAttribute("book",list1);
        List<CatalogModel> list = service.findAllCata();
        model.addAttribute("list",list);
        return "WEB-INF/view/index.jsp";
    }
    @RequestMapping(value = "list1",method = RequestMethod.POST)
    public String getByName(Model model,@RequestParam("bookname") String bookname, HttpServletRequest request){
        String username = (String)request.getSession().getAttribute("username");
        if(username == null){
            model.addAttribute("message","用户不存在请登录");
            return "WEB-INF/view/login.jsp";
        }
        List<BooksVo> list1 = bookService.findAllBooksByName(bookname);
        model.addAttribute("book",list1);
        List<CatalogModel> list = service.findAllCata();
        model.addAttribute("list",list);
        logger.info(list1.get(0).getBookName());
        return "WEB-INF/view/index.jsp";
    }
    @RequestMapping("userInfo")
    public String toUserInfo(@RequestParam String userid,Model model){
        UserModel userModel=userService.findByUserid(userid);
        model.addAttribute("user",userModel);
        return "WEB-INF/view/userInfo.jsp";
    }
    @RequestMapping("toGoods")
    public String toGoods(Model model,HttpServletRequest request){
        List<BooksVo> bookList = bookService.findAllBook();
        HttpSession session = request.getSession();
        UserModel user = userService.findByUser(session.getAttribute("username").toString());
        model.addAttribute("list",bookList);
        model.addAttribute("user",user);
        return "WEB-INF/view/goods.jsp";
    }
    @RequestMapping("toOrder")
    public String toOrder(Model model,HttpServletRequest request){
        List<OrderVo>cartList=cartService.getCartList();
        model.addAttribute("list",cartList);
        HttpSession session = request.getSession();
        UserModel user = userService.findByUser(session.getAttribute("username").toString());
        model.addAttribute("user",user);
        return "WEB-INF/view/order.jsp";
    }
    @RequestMapping("deleteOrderByOrderId")
    public String deleteOrderByOrderId(Model model,HttpServletRequest request,@RequestParam("orderId")String orderId){
        cartService.deleteOrder(orderId);
        List<OrderVo>cartList=cartService.getCartList();
        model.addAttribute("list",cartList);
        HttpSession session = request.getSession();
        UserModel user = userService.findByUser(session.getAttribute("username").toString());
        model.addAttribute("user",user);
        return "WEB-INF/view/order.jsp";
    }
    @RequestMapping("toBook")
    public String toBook(){
        return "WEB-INF/view/book.jsp";
    }
    @RequestMapping("addBook")
    public String addBook(BookModel book,Model model,HttpServletRequest request){
        bookService.add(book);
        HttpSession session = request.getSession();
        UserModel user = userService.findByUser(session.getAttribute("username").toString());
        List<BooksVo> bookList = bookService.findAllBook();
        model.addAttribute("list",bookList);
        model.addAttribute("user",user);
        return "WEB-INF/view/goods.jsp";
    }
    @RequestMapping("toEditBook")
    public String toEditBook(@RequestParam("bookId") String bookId,Model model){
        BookModel book= bookService.findBookById(bookId);
        model.addAttribute("book",book);
        return "WEB-INF/view/editBook.jsp";
    }
    @RequestMapping("editBook")
    public String editBook(HttpServletRequest request,BookModel book,Model model){
        bookService.updateBook(book);
        HttpSession session = request.getSession();
        UserModel user = userService.findByUser(session.getAttribute("username").toString());
        model.addAttribute("user",user);
        List<BooksVo> bookList = bookService.findAllBook();
        model.addAttribute("list",bookList);
        return "WEB-INF/view/goods.jsp";
    }
    @RequestMapping("deleteBookById")
    public String deleteBookById(HttpServletRequest request,@RequestParam("bookId") String bookId,Model model){
        bookService.deleteBookById(bookId);
        HttpSession session = request.getSession();
        UserModel user = userService.findByUser(session.getAttribute("username").toString());
        List<BooksVo> bookList = bookService.findAllBook();
        model.addAttribute("list",bookList);
        model.addAttribute("user",user);
        return "WEB-INF/view/goods.jsp";
    }
}
